﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using NetBay.Core.Silverlight.Tools;
using Rio.Framework.WCFService.RioWcfServices;
using System.ServiceModel;
using System.Collections.Generic;

namespace Rio.Framework.WCFService
{
    public static class ServiceManager
    {
        public static string RioWCFEndPoint { get; set; }
        public static string LEADTOOLSWCFEndPoint { get; set; }
        public static string OpenTimeout { get; set; }
        public static string SendTimeout { get; set; }
        public static string ReceiveTimeout { get; set; }
        public static string LicenseKey { get; set; }
        public static int LicenseKeyDuration { get; set; }
        public static int UploadThreadPoolMaxThreads { get; set; }
        public static uint UploadThreadTimeout { get; set; }
        public static int UploadThreadAttempt { get; set; }
        public static int UploadThreadInterval { get; set; }
        public static int FileMaxLength { get; set; }
        public static bool ImportOrderByDescending { get; set; }

        static ServiceManager()
        {
            LicenseKeyDuration = 10;
        }
        
        /// <summary>
        /// Provide custom proxy for RioWCFServices
        /// </summary>
        /// <returns></returns>
        public static WCFServiceClient GetRioWCFServiceClient()
        {
            string[] l_oHttpMode = RioWCFEndPoint.Split(':');
            BasicHttpBinding binding = new BasicHttpBinding(
                l_oHttpMode[0].Equals("https", StringComparison.InvariantCultureIgnoreCase)
                ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.MaxBufferSize = Int32.MaxValue;
            binding.ReceiveTimeout = new TimeSpan(0, Convert.ToInt32(ReceiveTimeout), 0);
            binding.SendTimeout = new TimeSpan(0, Convert.ToInt32(SendTimeout), 0);
            binding.OpenTimeout = new TimeSpan(0, Convert.ToInt32(OpenTimeout), 0);

            return new WCFServiceClient(binding, new EndpointAddress(new Uri(RioWCFEndPoint)));
        }

        /// <summary>
        /// Provide custom proxy for LEADTOOLSWCFServices
        /// </summary>
        /// <returns></returns>
        public static WCFServiceClient GetLEADTOOLSWCFServiceClient()
        {
            string[] l_oHttpMode = LEADTOOLSWCFEndPoint.Split(':');
            BasicHttpBinding binding = new BasicHttpBinding(
                l_oHttpMode[0].Equals("https", StringComparison.InvariantCultureIgnoreCase)
                ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.MaxBufferSize = Int32.MaxValue;
            binding.ReceiveTimeout = new TimeSpan(0, Convert.ToInt32(ReceiveTimeout), 0);
            binding.SendTimeout = new TimeSpan(0, Convert.ToInt32(SendTimeout), 0);
            binding.OpenTimeout = new TimeSpan(0, Convert.ToInt32(OpenTimeout), 0);
            return new WCFServiceClient(binding, new EndpointAddress(new Uri(LEADTOOLSWCFEndPoint))); 
        }

        /// <summary>
        /// Build the message request with the license key
        /// </summary>
        /// <returns></returns>
        public static MessageRequest GetLicenseKey()
        {
            string encryptedLicense = AesEncryption.Encrypt(string.Format("{0}${1}", LicenseKey, DateTime.Now.AddSeconds(LicenseKeyDuration).ToString())); 
            MessageRequest l_oMsgRequest = new MessageRequest
                                               {
                                                   LicenseKey = encryptedLicense,
                                               };
            return l_oMsgRequest;
        }

        
    }
}
